package com.ruoyi.framework.config;

import lombok.Data;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
@Configuration
@ConfigurationProperties(prefix = "wechat.cp")
public class WxCpConfiguration {

    private String corpId;
    private List<AppConfig> apps;

    @Data
    public static class AppConfig {
        private Integer agentId;
        private String secret;
        private String token;
        private String aesKey;
        private String archiveSecret;
        private String archiveLibPath;
        private String priKey;

    }

    /**
     * 主配置存储（使用第一个应用作为默认配置）
     */
    @Bean
    public WxCpConfigStorage wxCpConfigStorage() {
        if (apps == null || apps.isEmpty()) {
            throw new IllegalStateException("企业微信应用配置不能为空");
        }

        AppConfig defaultApp = apps.get(0);
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setCorpId(corpId);
        config.setAgentId(defaultApp.getAgentId());
        config.setCorpSecret(defaultApp.getSecret());
        config.setToken(defaultApp.getToken());
        config.setAesKey(defaultApp.getAesKey());
        config.setMsgAuditSecret(defaultApp.getArchiveSecret());
        config.setMsgAuditLibPath(defaultApp.getArchiveLibPath());
        config.setMsgAuditPriKey(defaultApp.getPriKey());
        return config;
    }

    /**
     * 默认的 WxCpService
     */
    @Bean
    public WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage) {
        WxCpService service = new WxCpServiceImpl();
        service.setWxCpConfigStorage(wxCpConfigStorage);
        return service;
    }

    /**
     * 多应用服务映射
     */
    @Bean
    public Map<Integer, WxCpService> wxCpServices() {
        Map<Integer, WxCpService> services = new ConcurrentHashMap<>();
        for (AppConfig appConfig : apps) {
            WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
            config.setCorpId(corpId);
            config.setAgentId(appConfig.getAgentId());
            config.setCorpSecret(appConfig.getSecret());
            config.setToken(appConfig.getToken());
            config.setAesKey(appConfig.getAesKey());
            config.setMsgAuditPriKey(appConfig.getArchiveSecret());
            config.setMsgAuditLibPath(appConfig.getArchiveLibPath());
            config.setMsgAuditSecret(appConfig.getPriKey());
            WxCpService service = new WxCpServiceImpl();
            service.setWxCpConfigStorage(config);
            services.put(appConfig.getAgentId(), service);
        }
        return services;
    }
}
